package cn.edu.szpt.qqdemov3;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import cn.edu.szpt.qqdemov3.adapters.TeamAdapter;
import cn.edu.szpt.qqdemov3.beans.QQContactBean;
import cn.edu.szpt.qqdemov3.beans.TeamBean;
import cn.edu.szpt.qqdemov3.net.MyHttpUrlConnection;
import cn.edu.szpt.qqdemov3.net.MyHttpUrlConnectionT1;
import cn.edu.szpt.qqdemov3.net.Result;

public class MainActivityNet extends AppCompatActivity {

    private Button btnNet;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
//            btnNet.setText("111");
            btnNet.setText(msg.getData().getString("data"));
        }
    };
    private TeamAdapter adapter;
    private ArrayList<TeamBean> beans;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_net);

        btnNet = findViewById(R.id.btn_net);
        Log.d("TAG", "run: " + Thread.currentThread());
        RecyclerView recycler = findViewById(R.id.recyclerView);


        beans = new ArrayList<>();
        adapter = new TeamAdapter(beans, this);


        btnNet.setOnClickListener(v -> {


            doAsnycTask();

        });


    }

    private void doAsnycTask() {
        MyHttpUrlConnection request = new MyHttpUrlConnection() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson = new Gson();

                System.out.println(s);
                Type type = new TypeToken<ArrayList<TeamBean>>() {
                }.getType();
                ArrayList<TeamBean> o = gson.fromJson(s, type);

                o.forEach(v->{
                    System.out.println(v.toString());

                });

            }
        };

        request.doGet("http://8.129.167.217/SoccerDataHandler.ashx?action=getTeamWithFlagList");
    }
}